//
//  PopoverAnimation.m
//  SanFangLianKong
//
//  Created by x on 2017/12/8.
//  Copyright © 2017年 zhaidanhui. All rights reserved.
//

#import "PopoverAnimation.h"
#import "XMGPopoverPresentationController.h"
@interface PopoverAnimation ()<UIViewControllerTransitioningDelegate,UIViewControllerAnimatedTransitioning>

@property (nonatomic, getter=isShowPresentView) BOOL showPresentView;
@end
@implementation PopoverAnimation

#pragma mark -- <UIViewControllerTransitioningDelegate,UIViewControllerAnimatedTransitioning>


/**
 实现代理 返回控制器的代理对象 谁来负责我们以后的转产动画
 
 @param presented 被展现的控制器
 @param presenting 发起控制器
 @param source <#source description#>
 @return <#return value description#>
 */
- (UIPresentationController *)presentationControllerForPresentedViewController:(UIViewController *)presented presentingViewController:(UIViewController *)presenting sourceViewController:(UIViewController *)source
{
    XMGPopoverPresentationController *popoverPresentationController = [[XMGPopoverPresentationController alloc] initWithPresentedViewController:presented presentingViewController:presenting];
    popoverPresentationController.popoverFrame = self.popoverFrame;
    
    return popoverPresentationController;
}

//如果你实现了这两个方法系统就不会在管理你的控制器出现消失动画  所有的东西都要你自己实现
/**
 告诉系统谁来执行model出现的动画  展现视图出现时会执行这个方法
 
 @param presented 被展现的视图控制器
 @param presenting 发起的视图控制器
 @param source <#source description#>
 @return 返回当前执行这个动作的执行者
 */
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source
{
    self.showPresentView = YES;
    return self;
}

/**
 告诉系统谁来执行model消失(dismiss)时的动画  展现视图消失时候回执行这个方法
 
 @param dismissed <#dismissed description#>
 @return 返回model视图dismiss时的动画执行者
 */
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
{
    self.showPresentView = NO;
    return self;
}
/**
 跳转动画的时间
 
 @param transitionContext 上文问 里面包含实行动画的所有东西
 @return 返回一个动画时间
 */
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext{
    
    return 0.1;
}


/**
 实现动画的具体操作
 
 @param transitionContext 上下文
 */
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext{
    
    if (self.isShowPresentView) {
        //model视图出现时候的动画
        //1.首先我们要拿到展现视图的View 或则控制器
        //        UIViewController *presentViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
        //        NSLog(@"=========-------%@",presentViewController);
        //展示图的view
        UIView *presentView = [transitionContext viewForKey:UITransitionContextToViewKey];
        //把视图的高度缩小到0
//        presentView.transform = CGAffineTransformMakeScale(1.0, 0);
//        presentView.layer.anchorPoint = CGPointMake(1.0, 0.0);
        
        //注意 : 要把视图添加到上下文中
        [transitionContext.containerView addSubview:presentView];
        
        //2.设置动画 然后执行动画
//        [UIView animateWithDuration:0.5 animations:^{
//            //恢复原状
//            presentView.transform = CGAffineTransformIdentity;
//        } completion:^(BOOL finished) {
            //当你动画执行完毕 一定要告诉系统你执行完动画了
            //如果不写这句 可能会发生一些未知的动画
            [transitionContext completeTransition:YES];
//        }];
    }else
    {
        //modal视图消失的动画
        UIView *presentView = [transitionContext viewForKey:UITransitionContextFromViewKey];
//        presentView.layer.anchorPoint = CGPointMake(1.0, 0);
//        [UIView animateWithDuration:0.5 animations:^{
            presentView.transform = CGAffineTransformMakeScale(1.0, 0.0000001);
//        } completion:^(BOOL finished) {
            [transitionContext completeTransition:YES];
//        }];
    }
}

@end
